package cn.jesin.css.controller;

import cn.jesin.css.common.Flag;
import cn.jesin.css.entity.Creation;
import cn.jesin.css.service.CreationService;
import cn.jesin.css.vo.CreationInfo;
import cn.jesin.css.vo.Page;
import cn.jesin.css.vo.responseformat.Code;
import cn.jesin.css.vo.responseformat.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author 谷汉斌
 * @description 作品控制器
 * @createTime 2024/8/21 上午9:40
 */
@Tag(name = "作品控制器", description = "处理作品相关请求")
@RestController
@RequestMapping("/creations")
public class CreationController {

    @Resource
    private CreationService creationService;


    /**
     * 添加作品
     */
    @Operation(summary = "添加作品")
    @PostMapping("/add")
    public Result<Object> addCreation(@RequestBody Creation creation) {
        if (creation != null) {
            boolean res = creationService.addCreation(creation);
            return Result.builder().flag(res ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                    .message(res ? Code.ADD_SUCCESS.getInfo() : Code.ADD_FAIL.getInfo())
                    .code(res ? Code.ADD_SUCCESS.getCode() : Code.ADD_FAIL.getCode())
                    .build();
        }
        return Result.builder().code(Code.ADD_FAIL.getCode())
                .flag(Flag.FAIL.isCode())
                .message(Code.ADD_FAIL.getInfo())
                .build();
    }

    /**
     * 删除作品
     */
    @Operation(summary = "删除作品")
    @PostMapping("/delete")
    public Result<Object> deleteCreation(@RequestParam Integer id) {
        boolean res = creationService.deleteCreation(id);
        return Result.builder().flag(res ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .message(res ? Code.DELETE_SUCCESS.getInfo() : Code.DELETE_FAIL.getInfo())
                .code(res ? Code.DELETE_SUCCESS.getCode() : Code.DELETE_FAIL.getCode())
                .build();
    }

    /**
     * 分页返回作品列表
     */
    @Operation(summary = "分页返回作品列表")
    @GetMapping("/list")
    public Result<Object> getCreationList(@RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                                          @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                          @RequestParam(value = "isFinish", defaultValue = "1") Integer isFinish,
                                          @RequestParam(value = "type", defaultValue = "文章") String type,
                                          @RequestParam(value = "keyword", defaultValue = "") String keyword) {
        Page<CreationInfo> pageInfo = creationService.getCreationList(page, pageSize, isFinish, type, keyword);

        return Result.builder().flag(Flag.SUCCESS.isCode()).code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo()).data(pageInfo).build();
    }


    /**
     * 修改作品
     */
    @Operation(summary = "修改作品")
    @PutMapping("")
    public Result<Object> updateCreation(@RequestBody Creation creation) {
        if (creation != null) {
            boolean res = creationService.updateCreation(creation);
            return Result.builder().flag(res ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                    .message(res ? Code.UPDATE_SUCCESS.getInfo() : Code.UPDATE_FAIL.getInfo())
                    .code(res ? Code.UPDATE_SUCCESS.getCode() : Code.UPDATE_FAIL.getCode()).build();
        }
        return Result.builder().code(Code.UPDATE_FAIL.getCode())
                .message(Code.UPDATE_FAIL.getInfo())
                .flag(Flag.FAIL.isCode())
                .build();
    }

    /**
     * 点赞，收藏，取消点赞，取消收藏
     */
    @Operation(summary = "点赞，收藏，取消点赞，取消收藏")
    @PutMapping("/operate")
    public Result<Object> like(@RequestParam Integer id,
                               @RequestParam String operate,
                               @RequestParam(value = "operateType", defaultValue = "add") String operateType,
                               @RequestBody Creation creation) {
        boolean res = creationService.operate(id, operate, operateType, creation);
        return Result.builder().flag(res ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .message(res ? Code.ADD_SUCCESS.getInfo() : Code.ADD_FAIL.getInfo())
                .code(res ? Code.ADD_SUCCESS.getCode() : Code.ADD_FAIL.getCode())
                .build();
    }
}
